package com.screen.screendisplay.service.impl.ne;

import com.screen.screendisplay.entity.ne.NeSafetyenvNetRank;
import com.screen.screendisplay.mapper.ne.NeSafetyenvNetRankMapper;
import com.screen.screendisplay.service.ne.NeSafetyenvNetRankService;
import com.screen.screendisplay.vo.ne.NePlatformRankingVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 互联网+平台应用排名表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeSafetyenvNetRankServiceImpl extends ServiceImpl<NeSafetyenvNetRankMapper, NeSafetyenvNetRank> implements NeSafetyenvNetRankService {

    @Override
    public List<NePlatformRankingVO> getPlatformRanking(Integer year) {
        System.out.println("=== getPlatformRanking called with year: " + year + " ===");
        
        // 构建查询条件
        QueryWrapper<NeSafetyenvNetRank> queryWrapper = new QueryWrapper<>();
        
        // 如果指定了年份，则只查询该年份的数据
        if (year != null) {
            queryWrapper.eq("year", year);
        }
        
        // 按年月倒序，然后按排名升序
        queryWrapper.orderByDesc("year", "month").orderByAsc("ranking");
        
        // 限制返回10条数据，与mock数据保持一致
        queryWrapper.last("LIMIT 10");
        
        List<NeSafetyenvNetRank> entities = this.list(queryWrapper);
        System.out.println("Found " + entities.size() + " entities from database");
        
        List<NePlatformRankingVO> resultList = new ArrayList<>();
        
        for (NeSafetyenvNetRank entity : entities) {
            NePlatformRankingVO vo = new NePlatformRankingVO();
            
            // 转换平台名称（Byte转String）
            vo.setPlatformName(convertPlatformName(entity.getPlatformName()));
            
            // 直接映射数值字段
            vo.setHiddenDangerCount(entity.getHiddenDangerCount());
            vo.setRiskCount(entity.getRiskCount());
            vo.setRectificationResult(entity.getRectificationResult());
            
            // 转换危险等级（Byte转String）
            vo.setDangerLevel(convertDangerLevel(entity.getDangerLevel()));
            
            resultList.add(vo);
        }
        
        System.out.println("Converted " + resultList.size() + " records to VOs");
        return resultList;
    }
    
    /**
     * 转换平台名称
     * 0-保力图风电场，1-棋盘山一期，2-棋盘山二期，3-甘泉山风电场
     */
    private String convertPlatformName(Byte platformName) {
        if (platformName == null) {
            return "未知平台";
        }
        
        switch (platformName) {
            case 0:
                return "保力图风电场";
            case 1:
                return "棋盘山一期";
            case 2:
                return "棋盘山二期";
            case 3:
                return "甘泉山风电场";
            default:
                return "未知平台";
        }
    }
    
    /**
     * 转换危险等级
     * 0-低，1-中，2-高
     */
    private String convertDangerLevel(Byte dangerLevel) {
        if (dangerLevel == null) {
            return "未知";
        }
        
        switch (dangerLevel) {
            case 0:
                return "低";
            case 1:
                return "中";
            case 2:
                return "高";
            default:
                return "未知";
        }
    }

}
